﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Generic;

namespace VSM.Parts
{
    public partial class FreightPopup : System.Web.UI.Page
    {
        private  GeneralFunctions GF = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                if (Session[Utils.AppConstants.ID_User] == null
                    || Session[Utils.AppConstants.Password] == null
                    || Session[Utils.AppConstants.ID_Dealer] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                string dbType = (string)Session[Utils.AppConstants.ID_Dealer] == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());
                if (!IsPostBack)
                {
                  
                    // Load Freight
                    LoadFreight();
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdBP_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                    e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)"); 
                    e.Row.Attributes["style"] = "cursor:pointer";

                    TextBox txtAmount =  (TextBox)e.Row.FindControl("txtAmount");
                    if (txtAmount != null) txtAmount.Text = GF.FormatNumeric(GF.ResetFormatNumeric(txtAmount.Text), "SumDec");
                }
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtAmount_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtAmount = (TextBox)row.FindControl("txtAmount");
                if (txtAmount.Text.Trim().Length == 0)
                {
                    ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "CallJS", "alert('Pls input Amount');", true);
                    txtAmount.Focus();
                    return;
                }

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblQUT3 = (DataTable)Session["FreightTable"];

                if (tblQUT3 != null)
                {
                    DataRow[] rupdate = tblQUT3.Select("No=" + lblNo.Text );
                    if (rupdate.Length > 0)
                    {
                        rupdate[0]["FeeValue"] = GF.FormatNumeric(GF.ResetFormatNumeric(txtAmount.Text), "SumDec");
                    }
                }

                this.grdFreight.EditIndex = -1;
                BindFreight(tblQUT3);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void txtComments_OnTextChanged(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                GridViewRow row = (GridViewRow)((DataControlFieldCell)((TextBox)sender).Parent).Parent;
                ViewState["rowindex"] = row.RowIndex;
                TextBox txtComments = (TextBox)row.FindControl("txtComments");

                Label lblNo = (Label)row.FindControl("lblNo");
                DataTable tblQUT3 = (DataTable)Session["FreightTable"];

                if (tblQUT3 != null)
                {
                    DataRow[] rupdate = tblQUT3.Select("No=" + lblNo.Text);
                    if (rupdate.Length > 0)
                    {
                        rupdate[0]["Comments"] = txtComments.Text.Trim();
                    }
                }

                this.grdFreight.EditIndex = -1;
                BindFreight(tblQUT3);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                this.lblError.Visible = false;
                Session["FreightTable"] = Session["FreightTable"];
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('AddFreight');", true);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

        private void LoadFreight()
        {
            try
            {
                this.lblError.Visible = false;
                DataTable dtQUT3 = (DataTable)Session["FreightTable"];
                BindFreight(dtQUT3);
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;            
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindFreight(DataTable tblQUT3)
        {
            try
            {
                this.lblError.Visible = false;
                Session["FreightTable"] = tblQUT3;
                this.grdFreight.DataSource = tblQUT3;
                this.grdFreight.DataBind();
            }
            catch (Exception ex)
            {
                this.lblError.Text = ex.Message;
                this.lblError.Visible = true;
            }
        }

    }
}